/*
 * gbdt_tree.h
 *
 *  Created on: 2012-7-21
 *      Author: wanghaox
 */

#ifndef GBDT_TREE_H_
#define GBDT_TREE_H_

#include <math.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "gbdt_feature.h"
#include "gbdt_param.h"

//using namespace std;

struct TreeNode {
	int feature;
	float split;
	double label, loss;

	int m_infty, m_s;
	float s;
	double l_infty, l_s;

	int level;
	int seq;

	void reset() {
		feature = -1;
		split = -1.f;
		label = -1.0;
		loss = -1.0;

		m_infty = 0;
		l_infty = 0.0;
	}
};

class GbdtTree {
public:
	GbdtTree() {
	}

	~GbdtTree() {
		uninit();
	}

	bool init(int depth);
	void uninit();

	void clear();

	void find_local_split(GbdtParam &global_param, GbdtFeature &feature);
	void update_split(GbdtParam &global_param, GbdtFeature &feature);
	void curren_layer_to_buffer(string &buffer);

	void print();
	void print_screen();
private:
	void print_node(int level, int i, double learningrate);
private:
	TreeNode*** _layers;
	int _depth, _layer, _nodes;

	inline int nodes_at_depth(int d) {
		return (int) pow(2.f, ((float) d));
	}

	inline void start_next_layer() {
		// increment layer
		_layer++;
		_nodes = (int) pow(2.f, ((float) _layer));
	}


//public:
//	// construction methods
//	void clear();
//	void startNextLayer();
//    void findBestLocalSplits(FeatureData* data);
//    void exchangeBestSplits();
//    bool containsSplittingFeature(FeatureData* data);
//
//	// prediction methods
//    void updateTrainingPredictions(FeatureData *data, double learningrate);
//    void updatePredictions(InstanceData *data, double learningrate);
//
//    // output methods
//    void printTree(double learningrate);
//
//	// info methods
//	void getSplit(int node, int &feature, float &split);
//    int getNumNodes();
//
//private:
//	StaticNode*** layers;
//	int depth, layer, nodes;
//
//    int nodesAtDepth(int d);
//    void clearNode(StaticNode* node);
//    double classifyDataPoint(InstanceData* data, int p);
//	void updateBestSplits(FeatureData* data, int f);
//    void printNode(int level, int i, double learningrate);
};

#endif /* GBDT_TREE_H_ */
